package wortubung.wordlist;

import java.io.File;
import java.util.*;

import wortubung.WortUbungException;
import wortubung.core.LANG;
import wortubung.core.WORD;

public class WordListInfo implements Comparable<WordListInfo> {
	private File file;
    private String fileName;
    private String name;
    private String shortName;
    private LANG lang;

    List<WORD> words;

    public WordListInfo(File file, LANG lang, String name, String shortName) {
    	this(file, null, lang, name, shortName);
    }

    public WordListInfo(String fileName, LANG lang, String name, String shortName) {
    	this(null, fileName, lang, name, shortName);
    }

    private WordListInfo(File file, String fileName, LANG lang, String name, String shortName) {
        words = new ArrayList<WORD>();

        this.file = file;
        this.fileName = file != null ? file.getName() : fileName;
        this.name = name;
        this.shortName = shortName;

        String fmt = "File " + fileName + " does not have '%s' set.";
        if (this.name == null)
            throw new WortUbungException(String.format(fmt, "name"));
        if (this.shortName == null)
            throw new WortUbungException(String.format(fmt, "shortname"));
        if (lang == null)
            throw new WortUbungException(String.format(fmt, "lang"));
        this.lang = lang;
    }

    public boolean isBuiltin() { return file == null; }
    public File getFile() { return file; }
    public String getFileName() { return fileName; }
    public LANG getLang() { return lang; }
    public String getName() { return name; }
    public String getShortName() { return shortName; }
    public List<WORD> getWords() {return words; }
    public int numberOfAllWords() { return words.size(); }
    public void addWord(WORD word) { words.add(word); }

    public String[] getPrefices(int prefixLen) {
    	HashSet<String> set = new HashSet<String>();
		for (WORD wd : getWords()) {
			String s = wd.getWord();
			if (s.length() <= prefixLen)
				continue;
			if (set == null)
				set = new HashSet<String>();
			StringBuilder sb = new StringBuilder();
			for (int i=0; sb.length() < prefixLen && i < s.length(); ++i) {
				char c = s.charAt(i);
				if (c != '-' && c != '\'' && c != '.')
					sb.append(lang.toLatin(c, false));
			}
			set.add(sb.toString().toLowerCase());
		}
		String[] sa = set.toArray(new String[set.size()]);
		Arrays.sort(sa);
		return sa;
    }

    public Map<String,Integer> getPrefixCounts(int prefixLen) {
    	HashMap<String,Integer> map = new HashMap<String,Integer>();
		for (WORD wd : getWords()) {
			String s = wd.getWord();
			if (s.length() <= prefixLen)
				continue;
			s = lang.toLatin(s.substring(0, prefixLen), false).toLowerCase();
			Integer cnt = map.get(s);
			if (cnt == null)
				cnt = 0;
			map.put(s, cnt + 1);
		}
		return map;
    }

    public int numberOfOriginalWords() {
        int cnt = 0;
        for (WORD wd : words)
        	if (!wd.isDerived())
        		++cnt;
        return cnt;
    }

	@Override
	public int compareTo(WordListInfo other) {
		return getShortName().compareTo(other.getShortName());
	}
}
